package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.bumptech.glide.Glide
import de.hdodenhof.circleimageview.CircleImageView
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.javaBean.MallBean
import fhp.hlhj.giantfold.network.Urls

/**
 * author: FHP
 * data: on 2018\1\16 0016 14:13
 * E-Mail: 103945089@qq.com
 */
class MallHeaderGridAdp(private val context:Context,private val datas:List<MallBean.DataBean>):BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView =convertView
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.tabbao_header_item,null)
        }
        val tittle = convertView?.findViewOften<TextView>(R.id.itemTitle)
        val image = convertView?.findViewOften<CircleImageView>(R.id.itemIcon)
        val data = datas[position]
        tittle?.text = data.name

        Glide.with(context).load(Urls.BaseUrl+data.savepath+data.savename).asBitmap().into(image)
        return convertView
    }

    override fun getItem(position: Int): Any {
       return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return datas.size
    }


    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}